<?php
namespace app\api\controller;

use think\Request;

class World extends Base
{   
    
    ##首页
    public function Index()
    {
       
        $day = date("Y-m-d");
        $time = date("H:i");

        //首页封面图
        $res['img'] = db('world_config')->field('image,img')->find(); 
        $res['img']['image'] = $this->world_img($res['img']['image']);
        $res['img']['img'] = $this->world_img($res['img']['img']);

        //获取今日球队
        $res['team'] = db('world_cace')->where(['date'=>$day,'time'=>['gt',$time]])->select();
        
        //查询球队信息
        $arr = db('world_team')->select();
        
        foreach ($res['team'] as $k => $v){
            foreach ($arr as $key => $val){
                if ($v['team_id_a'] == $val['id']){
                    $res['team'][$k]['team_id_a'] = $val['name'];
                    $res['team'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
                
                if ($v['team_id_b'] == $val['id']){
                    $res['team'][$k]['team_id_b'] = $val['name'];
                    $res['team'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
        }
        
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##比赛时间
    public function Competition()
    {
        $day = date("Y-m-d");
        $time = date("H:i");
        $res = db('world_cace')->where(['date'=>['gt',$day],'time'=>['gt',$time]])->field('date')->group('date')->select();

        //判断用的数组
        $my_array = ['A1','A2','B1','B2','C1','C2','D1','D2','E1','E2','F1','F2','G1','G2','H1','H2','49胜者','50胜者','53胜者','54胜者',
            '51胜者','52胜者','55胜者','56胜者','57胜者','58胜者','59胜者','60胜者','61负者','62负者','61胜者','62胜者'
        ];
        foreach ($res as $key => $val){
            
            $time = strtotime($val['date']);
            $res[$key]['w'] = date('w', $time);
            $res[$key]['team'] = db('world_cace')->where(['date'=>$val['date']])->select();
            
            
        }

        
        foreach ($res as $k => $v){
            
            
            foreach ($v['team'] as $key => $val){
                

                if ($val['team_id_a'] == null){
                    $res[$k]['team'][$key]['team_id_a_type'] = 1;
                }else{
                    $res[$k]['team'][$key]['team_id_a_type'] = 0;
                }
                
                if ($val['team_id_b'] == null){
                    $res[$k]['team'][$key]['team_id_b_type'] = 1;
                }else{
                    $res[$k]['team'][$key]['team_id_b_type'] = 0;
                }
                
                if ($val['victory'] == null){
                    $res[$k]['team'][$key]['statr'] = 1;
                }else{
                    $res[$k]['team'][$key]['statr'] = 0;
                }
            }
        }
        
        //查询球队信息
        $arr = db('world_team')->select();
        
        foreach ($res as $k => $v){
            foreach ($v['team'] as $kk => $vv){
                $res[$k]['team'][$kk]['team_id_a_flag'] = '';
                $res[$k]['team'][$kk]['team_id_b_flag'] = '';
                foreach ($arr as $key => $val){
                    
                    if ($vv['team_id_a'] == $val['id']){
                        $res[$k]['team'][$kk]['team_id_a'] = $val['name'];
                        $res[$k]['team'][$kk]['team_id_a_flag'] = $this->world_img($val['image']);
                    }
                
                    if ($vv['team_id_b'] == $val['id']){
                        $res[$k]['team'][$kk]['team_id_b'] = $val['name'];
                        $res[$k]['team'][$kk]['team_id_b_flag'] = $this->world_img($val['image']);
                    }
                }
            }
            
        }
  
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##进阶图
    public function Advanced_graph()
    {
        //所有球队
        $arr = db('world_team')->select();
        
        $my_array = ['A1','A2','B1','B2','C1','C2','D1','D2','E1','E2','F1','F2','G1','G2','H1','H2','49胜者','50胜者','53胜者','54胜者',
            '51胜者','52胜者','55胜者','56胜者','57胜者','58胜者','59胜者','60胜者','61负者','62负者','61胜者','62胜者'
        ];
        
        //1/8决赛上
        $res['a1'] = db('world_cace')->where(['match'=>"1/8决赛"])->field('team_id_a,team_id_b,score,type')->order('date asc')->limit(0,4)->select();
        
        foreach ($res['a1'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['a1'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['a1'][$k]['team_id_b'] = "";
            }
            
            $res['a1'][$k]['team_id_a_flag'] = "";
            $res['a1'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['a1'][$k]['team_id_a'] = $val['name'];
                    $res['a1'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
                
                if($v['team_id_b'] == $val['id']){
                    $res['a1'][$k]['team_id_b'] = $val['name'];
                    $res['a1'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
        }
        
        //1/8决赛下
        $res['a2'] = db('world_cace')->where(['match'=>"1/8决赛"])->field('team_id_a,team_id_b,score,type')->order('date asc')->limit(4,4)->select();
        
        foreach ($res['a2'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['a2'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['a2'][$k]['team_id_b'] = "";
            }
            
            $res['a2'][$k]['team_id_a_flag'] = "";
            $res['a2'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['a2'][$k]['team_id_a'] = $val['name'];
                    $res['a2'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
                
        
                if($v['team_id_b'] == $val['id']){
                    $res['a2'][$k]['team_id_b'] = $val['name'];
                    $res['a2'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
                
            }
        }
        
        //调换方向
        foreach ($res['a2'] as $k => $v){
            $a = $v['team_id_b'];
            $a_img = $v['team_id_b_flag'];
            $b = $v['team_id_a'];
            $b_img = $v['team_id_a_flag'];
        
            if ($v['type'] == 1){
                $res['a2'][$k]['type'] = 2;
            }elseif($v['type'] == 2){
                $res['a2'][$k]['type'] = 1;
            }
        
            $res['a2'][$k]['team_id_a'] = $a;
            $res['a2'][$k]['team_id_a_flag'] = $a_img;
            $res['a2'][$k]['team_id_b'] = $b;
            $res['a2'][$k]['team_id_b_flag'] = $b_img;
        
            if (!empty($v['score'])){
                $score_a = substr($v['score'] , 0 , 1);
                $score_b = substr($v['score'] , 2 , 1);
                $res['a2'][$k]['score'] = $score_b.'-'.$score_a;
        
            }
        
        
        
        }
        
        //1/4决赛上
        $res['b1'] = db('world_cace')->where(['match'=>"1/4决赛"])->field('team_id_a,team_id_b,score,type')->order('date asc')->limit(0,2)->select();
        
        foreach ($res['b1'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['b1'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['b1'][$k]['team_id_b'] = "";
            }
            
            $res['b1'][$k]['team_id_a_flag'] = "";
            $res['b1'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['b1'][$k]['team_id_a'] = $val['name'];
                    $res['b1'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
        
                if($v['team_id_b'] == $val['id']){
                    $res['b1'][$k]['team_id_b'] = $val['name'];
                    $res['b1'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
        }
        
        //1/4决赛下
        $res['b2'] = db('world_cace')->where(['match'=>"1/4决赛"])->field('team_id_a,team_id_b,score,type')->order('date asc')->limit(2,2)->select();
        
        foreach ($res['b2'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['b2'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['b2'][$k]['team_id_b'] = "";
            }
            $res['b2'][$k]['team_id_a_flag'] = "";
            $res['b2'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['b2'][$k]['team_id_a'] = $val['name'];
                    $res['b2'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
        
                if($v['team_id_b'] == $val['id']){
                    $res['b2'][$k]['team_id_b'] = $val['name'];
                    $res['b2'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
        }
        
        //调换方向
        foreach ($res['b2'] as $k => $v){
            $a = $v['team_id_b'];
            $a_img = $v['team_id_b_flag'];
            $b = $v['team_id_a'];
            $b_img = $v['team_id_a_flag'];
        
            if ($v['type'] == 1){
                $res['b2'][$k]['type'] = 2;
            }elseif($v['type'] == 2){
                $res['b2'][$k]['type'] = 1;
            }
        
            $res['b2'][$k]['team_id_a'] = $a;
            $res['b2'][$k]['team_id_a_flag'] = $a_img;
            $res['b2'][$k]['team_id_b'] = $b;
            $res['b2'][$k]['team_id_b_flag'] = $b_img;
        
            if (!empty($v['score'])){
                $score_a = substr($v['score'] , 0 , 1);
                $score_b = substr($v['score'] , 2 , 1);
                $res['b2'][$k]['score'] = $score_b.'-'.$score_a;
        
            }
        
        
        
        }
        
        
        //半决赛上
        $res['c1'] = db('world_cace')->where(['match'=>"半决赛"])->field('team_id_a,team_id_b,score,type')->order('date asc')->limit(0,1)->select();
        
        foreach ($res['c1'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['c1'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['c1'][$k]['team_id_b'] = "";
            }
            
            $res['c1'][$k]['team_id_a_flag'] = "";
            $res['c1'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['c1'][$k]['team_id_a'] = $val['name'];
                    $res['c1'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
            
                if($v['team_id_b'] == $val['id']){
                    $res['c1'][$k]['team_id_b'] = $val['name'];
                    $res['c1'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
            
        }
        
        
        //半决赛下
        $res['c2'] = db('world_cace')->where(['match'=>"半决赛"])->field('team_id_a,team_id_b,score,type')->order('date asc')->limit(1,1)->select();
        
        foreach ($res['c2'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['c2'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['c2'][$k]['team_id_b'] = "";
            }
            
            $res['c2'][$k]['team_id_a_flag'] = "";
            $res['c2'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['c2'][$k]['team_id_a'] = $val['name'];
                    $res['c2'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
            
                if($v['team_id_b'] == $val['id']){
                    $res['c2'][$k]['team_id_b'] = $val['name'];
                    $res['c2'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
        }
        
        //调换方向
        foreach ($res['c2'] as $k => $v){
            $a = $v['team_id_b'];
            $a_img = $v['team_id_b_flag'];
            $b = $v['team_id_a'];
            $b_img = $v['team_id_a_flag'];
            
            if ($v['type'] == 1){
                $res['c2'][$k]['type'] = 2;
            }elseif($v['type'] == 2){
                $res['c2'][$k]['type'] = 1;
            }
            
            $res['c2'][$k]['team_id_a'] = $a;
            $res['c2'][$k]['team_id_a_flag'] = $a_img;
            $res['c2'][$k]['team_id_b'] = $b;
            $res['c2'][$k]['team_id_b_flag'] = $b_img;
            
            if (!empty($v['score'])){
                $score_a = substr($v['score'] , 0 , 1);
                $score_b = substr($v['score'] , 2 , 1);
                $res['c2'][$k]['score'] = $score_b.'-'.$score_a;

            }
            
            
            
        }
        
        
        //决赛
        $res['d'] = db('world_cace')->where(['match'=>"决赛"])->field('team_id_a,team_id_b,score,type')->select();
        
        foreach ($res['d'] as $k => $v){
            if (in_array($v['team_id_a'],$my_array)){
                $res['d'][$k]['team_id_a'] = "";
            }
            if (in_array($v['team_id_b'],$my_array)){
                $res['d'][$k]['team_id_b'] = "";
            }
            
            $res['d'][$k]['team_id_a_flag'] = "";
            $res['d'][$k]['team_id_b_flag'] = "";
            foreach ($arr as $key => $val){
                if($v['team_id_a'] == $val['id']){
                    $res['d'][$k]['team_id_a'] = $val['name'];
                    $res['d'][$k]['team_id_a_flag'] = $this->world_img($val['image']);
                }
            
                if($v['team_id_b'] == $val['id']){
                    $res['d'][$k]['team_id_b'] = $val['name'];
                    $res['d'][$k]['team_id_b_flag'] = $this->world_img($val['image']);
                }
            }
        }
        
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
 
    
    ##积分榜
    public function integral()
    {
        //A组
        $res['A'] = db('world_ring')->alias('a')
                    ->join('world_team b','a.name = b.id','left')
                    ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
                    ->order('sort desc')
                    ->where(['a.type'=>"A"])->select();
        
                    foreach ($res['A'] as $k => $v){
                        $res['A'][$k]['image'] = $this->world_img($v['image']);
                    }
                
        //B组
        $res['B'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"B"])->select();
        
        foreach ($res['B'] as $k => $v){
            $res['B'][$k]['image'] = $this->world_img($v['image']);
        }
        
        //C组
        $res['C'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"C"])->select();
        
        foreach ($res['C'] as $k => $v){
            $res['C'][$k]['image'] = $this->world_img($v['image']);
        }
        
        //D组
        $res['D'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"D"])->select();
        
        foreach ($res['D'] as $k => $v){
            $res['D'][$k]['image'] = $this->world_img($v['image']);
        }
        
        //E组
        $res['E'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"E"])->select();
        
        foreach ($res['E'] as $k => $v){
            $res['E'][$k]['image'] = $this->world_img($v['image']);
        }
        
        //F组
        $res['F'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"F"])->select();
        
        foreach ($res['F'] as $k => $v){
            $res['F'][$k]['image'] = $this->world_img($v['image']);
        }
        
        //G组
        $res['G'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"G"])->select();
        
        foreach ($res['G'] as $k => $v){
            $res['G'][$k]['image'] = $this->world_img($v['image']);
        }
        
        //H组
        $res['H'] = db('world_ring')->alias('a')
        ->join('world_team b','a.name = b.id','left')
        ->field('b.name,b.image,a.numbers,a.win,a.flat,a.negative,a.record,a.integral')
        ->order('sort desc')
        ->where(['a.type'=>"H"])->select();
        
        foreach ($res['H'] as $k => $v){
            $res['H'][$k]['image'] = $this->world_img($v['image']);
        }
        
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##比赛预测页面
    public function Forecast_list()
    {
        $post = request()->only(['id','type'],'post');
        
        
        if (empty($post['type'])){
            err('type not null');
        }elseif($post['type'] == 1 || $post['type'] == 2){
            if (empty($post['id'])){
                err('id not null');
            }
            $where['team_id'] = $post['id'];
        }
        
        
        
        //所有球队
        $arr = db('world_team')->select();
        
        $where['user_id'] = $this->accessData['uid'];
        $where['type'] = $post['type'];

        $res = db('world_forecast')
                ->field('score_a,score_b,win,champion')
                ->where($where)->find();
        
        foreach ($arr as $k => $v){
                if ($res['win'] == $v['name']){
                    $res['win'] = $v['id'];
                }
        }

        if ($post['type'] == 2){
            $str = db('world_cace')->field('team_id_a,team_id_b')->where(['id'=>$post['id']])->find();
 
            if ($res['win'] == $str['team_id_a']){
                $res['type'] = 1;
            }elseif ($res['win'] == $str['team_id_b']){
                $res['type'] = 2;
            }
        
        }
                
        foreach ($arr as $k => $v){
            if ($res['win'] == $v['id']){
                $res['win'] = $v['name'];
            }
            
            if ($res['champion'] == $v['id']){
                $res['champion'] = $v['name'];
                $res['img'] = $this->world_img($v['image']);
            }
        }
        
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
        
    }
    
    ##猜胜负页面
    public function Victory()
    {
        $post = request()->only(['id'],'post');
        
        if (empty($post['id'])){
            err('id not null');
        }
        
        //所有球队
        $arr = db('world_team')->select();
        
        $res = db('world_cace')->field('team_id_a,team_id_b,time,match')->where(['id'=>$post['id']])->find();
        
        foreach ($arr as $k => $v){
            if ($res['team_id_a'] == $v['id']){
                $res['team_id_a'] = $v['name'];
                $res['team_id_a_img'] = $this->world_img($v['image']);
            }
            if ($res['team_id_b'] == $v['id']){
                $res['team_id_b'] = $v['name'];
                $res['team_id_b_img'] = $this->world_img($v['image']);
            }
        }
        $res['team_id'] = "平";
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##冠军预测所有球队
    public function Victory_list()
    {

        //所有球队
        $res = db('world_team')->field('image,name')->select();
        
        foreach ($res as $k => $v){
            $res[$k]['image'] = $this->world_img($v['image']);
        }
        
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
    
    ##分享图片和文字
    public function image()
    {
        $res = db('configure')->where(['admin_id'=>$this->accessData['aid']])->field('share_title,share_img,home_page')->find();
        
        $res['share_img'] = $this->world_img($res['share_img']);
        if ($res){
            suc('success',$res);
        }else{
            err('暂无数据');
        }
    }
    
    ##猜比分提交
    public function Score()
    {
        $post = request()->only(['id','score_a','score_b'],'post');
    
        if (empty($post['id'])){
            err('id not null');
        }
        if (empty($post['score_a'])){
            err('score_a not null');
        }
        if (empty($post['score_b'])){
            err('score_b not null');
        }
    
        if ($post['score_a'] == '-1'){
            $data['score_a'] = '0';
        }else{
            $data['score_a'] = $post['score_a'];
        }
        
        if ($post['score_b'] == '-1'){
            $data['score_b'] = '0';
        }else{
            $data['score_b'] = $post['score_b'];
        }
        
        $data['user_id'] = $this->accessData['uid'];
        $data['team_id'] = $post['id'];
        $data['type'] = 1;
        
        $res = db('world_forecast')->insert($data);
        if ($res){
            suc('ok');
        }else{
            err('提交失败');
        }
    }
    
    ##猜胜负提交
    public function Win()
    {
        $post = request()->only(['id','name'],'post');
    
        if (empty($post['name'])){
            err('name not null');
        }
        
        if (empty($post['id'])){
            err('id not null');
        }

        //所有球队
        $res = db('world_team')->field('id,image,name')->select();
        
        foreach ($res as $k => $v){
            if ($v['name'] == $post['name']){
                $data['win'] = $v['id'];
            }
        }
        
        $data['win'] = $post['name'];
        $data['user_id'] = $this->accessData['uid'];
        $data['team_id'] = $post['id'];
        $data['type'] = 2;
    
        $res = db('world_forecast')->insert($data);
        if ($res){
            suc('ok');
        }else{
            err('提交失败');
        }
    }
    
    ##猜冠军提交
    public function Champion()
    {
        $post = request()->only(['name'],'post');
    
        if (empty($post['name'])){
            err('name not null');
        }
    
    
        //所有球队
        $res = db('world_team')->field('id,image,name')->select();
    
        foreach ($res as $k => $v){
            if ($v['name'] == $post['name']){
                $data['champion'] = $v['id'];
            }
        }
    
        $data['user_id'] = $this->accessData['uid'];

        $data['type'] = 3;
    
        $res = db('world_forecast')->insert($data);
        if ($res){
            suc('ok');
        }else{
            err('提交失败');
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   
    
    
    
}